﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using NHibernate;
using BrokerBase.Lib.Client;
using BrokerBase.Lib.Tool;
using Broker.Update;

namespace Broker
{
    /// <summary>
    /// Interaction logic for ScoreForm.xaml
    /// </summary>
    public partial class ScoreForm
    {
        private IList<ScoreRecordInfo> scoreInfos;

        public ScoreForm()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ScoreForm_Loaded);
        }

        void ScoreForm_Loaded(object sender, RoutedEventArgs e)
        {
            AppGlobal.DataWCF.ConnectStateChanged += new ConnectStateChangedHandler(DataWCF_ConnectStateChanged);
            AppGlobal.DataWCF.ClientRegInfoChanged += new ClientRegInfoChangedHandler(DataWCF_ClientRegInfoChanged);
            AppGlobal.DataWCF.ClientScoreReceived += new ClientScoreInfoReceivedHandler(DataWCF_ClientScoreReceived);

            LoadData();
            SetInviteState();
        }

        private void btnInvite_Click(object sender, RoutedEventArgs e)
        {
            InvitedForm frm = new InvitedForm();

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = AppGlobal.MainWin;
            frm.ShowDialog();
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            using (ISession sess = AppGlobal.HbmSessionFactory.OpenSession())
            {
                LoadData();
                AppGlobal.DataWCF.IsNewReg = false;
                grdPrompt.Visibility = Visibility.Collapsed;
            }
        }

        private void LoadData()
        {

            scoreInfos = DBServiceUtil.GetService<ScoreRecordInfo>(AppGlobal.HbmSessionFactory.GetCurrentSession()).GetListByHQL("from ScoreRecordInfo as s Order by s.ScoreTime Desc").ToList<ScoreRecordInfo>();

            gvScoreInfo.ItemsSource = scoreInfos;

            txtScore.Text = scoreInfos.Sum(p => p.Score).ToString();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void DataWCF_ConnectStateChanged(RemoteDataServiceState state)
        {
            this.Dispatcher.Invoke(new Action(SetInviteState));
        }

        private void SetInviteState()
        {
            switch (AppGlobal.DataWCF.State)
            {
                case RemoteDataServiceState.NoConnect:
                    tbkState.Text = "没有连接到服务器，或服务器连接出现异常。";
                    break;
                case RemoteDataServiceState.Connecting:
                    tbkState.Text = "正在连接服务器,请等待，大概需要1到5分钟。";
                    break;
                case RemoteDataServiceState.Received:
                    SetInviteQty();
                    break;
            }
        }

        private void SetInviteQty()
        {
            int invQty = AppGlobal.ResultClientRegInfo.InviteQty;
            if (invQty > 0)
            {
                tbkState.Text = "您还有" + invQty + "个可用邀请码。";
                btnInvite.IsEnabled = true;
            }
            else
            {
                tbkState.Text = "您已经没有可用邀请码！";
                btnInvite.IsEnabled = false;
            }

            txtInviteBtn.Text = string.Format("邀请您的朋友一起使用!还剩({0})个注册码", invQty);
        }

        void DataWCF_ClientScoreReceived()
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                grdPrompt.Visibility = Visibility.Visible;
            }));
        }

        void DataWCF_ClientRegInfoChanged(ClientDataInfo info)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                SetInviteQty();
            }));
        }


    }
}
